package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.sercive.StudentService;

import java.util.Scanner;

public abstract class BaseStudentController {
    private StudentService studentService = new StudentService();
    private Scanner sc = new Scanner(System.in);
    //开启学生管理QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
    public void start() {
        studentLoop:while(true) {
            System.out.println("--------欢迎使用黑马<学生>管理系统--------");
            System.out.println("请输入您的选择:1.添加学生 2.删除学生  3.修改学生  4.查询学生 5.退出");
            String s = sc.next();
            switch (s) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查询学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统");
                    //return; //return是跳出while循环
                    break studentLoop;//使用此方法时,一定要在while前做一个标记(标记名可以随便起),此处跳出标记的while循环
                    //以上两种方法都可以
                    //break;//此处不能用break,break是跳出switch语句
                default:
                    System.out.println("您的输入有误,请重新输入!");
            }
        }

    }
    //修改学生信息
    public void updateStudent() {
        /*String updateId;
        while (true) {
            System.out.println("请输入您要修改的学生学号id");
            updateId = sc.next();
            //2.判断学生id是否存在,如果不存在,则继续录入
            boolean exists = studentService.isExists(updateId);
            if (!exists){
                System.out.println("您输入的学生学号id不存在,请重新输入!");
            }else{
                break;
            }
        }*/
        //上面的代码优化,调用inputStudentId方法
        String updateId = inputStudentId();

        /*System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年龄");
        int age = sc.nextInt();
        System.out.println("请输入学生生日");
        String birthday = sc.next();
        Student newStu = new Student();
        newStu.setId(updateId);
        newStu.setName(name);
        newStu.setAge(age);
        newStu.setBirthday(birthday);*/
        Student newStu = inputStudentInfo(updateId);

        boolean b = studentService.updateStudent(updateId,newStu);
    }

    //根据键盘录入的id删除学生的方法
    public void deleteStudentById() {
        //优化后的代码(调用inputStudentId方法)
        String delId = inputStudentId();

        //3.根据录入的学生id,调用业务员,删除学生
        boolean b = studentService.deleteStudentById(delId);
        if (b){
            System.out.println("删除成功");
        }

    }

    //查询学生的方法
    public void findAllStudent() {

        Student[] stus = studentService.findAllStudent();
       //判断数组是否为null
        if (stus == null){
            System.out.println("没有学生信息,请添加后重试!");
            return;
        }
        //打印表头信息
        System.out.println("学号\t\t\t姓名\t\t年龄\t\t生日");
        //不为空,遍历数组并打印学生信息
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student!=null){
                System.out.println(student.getId()+"\t"+student.getName()+"\t\t"+student.getAge()+"\t"+student.getBirthday());
            }
        }
    }


    //添加学生
    public void addStudent() {
        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {
            //键盘输入学生信息
            System.out.println("请输入学生学号:");
            id = sc.next();
            //2.判断学生id是否存在,如果不存在,则继续录入
            boolean exists = studentService.isExists(id);
            if (exists) {
                System.out.println("学号id存在,请重新输入!");
            }else{
                break;
            }
        }
        /*System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        //创建学生对象并赋值
        Student stu = new Student();
        stu.setId(id);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);*/
        //以上代码优化,调用方法inputStudentInfo获取学生对象
        Student stu = inputStudentInfo(id);
        //调用业务员把学生对象传到仓管
        boolean result = studentService.addStudent(stu);
        //根据获得的结果判断是否添加成功
        if (result){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    //键盘录入学生id
    public String inputStudentId(){
        String id;
        while(true) {
            //1.键盘录入学生id
            System.out.println("请输入学生id:");
            id = sc.next();
            //2.判断学生id是否存在,如果不存在,则继续录入
            boolean exists = studentService.isExists(id);
            if (!exists) {
                System.out.println("学号id不存在,请重新输入!");
            } else {
                break;
            }
        }
        return id;
    }
    //键盘录入学生信息
    public abstract Student inputStudentInfo(String id);
}
